package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.Image;
import cn.edu.sig.dao.ImageDao;
@Repository("imageDao")
public class ImageDaoImpl extends TemplateSupport implements ImageDao {

	public void add(Image image) {
		
		String sql = this.sqlSource.getSql("image.add");
		
		Object[] param = new Object[]{image.getUrl(),image.getGoods().getGoodsId()};
		
		this.getJdbcTemplate().update(sql, param);

	}

	public void delete(Integer imageId) {
		
		String sql = this.sqlSource.getSql("image.delete");
		
		this.getJdbcTemplate().update(sql,imageId);

	}

	public void edit(Image image) {
		
		String sql = this.sqlSource.getSql("image.add");
		
		Object[] param = new Object[]{image.getUrl(),image.getImageId()};
		
		this.getJdbcTemplate().update(sql, param);

	}

	public List<Image> findAll() {
		String sql = this.sqlSource.getSql("image.list");
		RowMapper<Image> mapper = new RowMapper<Image>() {
			public Image mapRow(ResultSet rs, int rowNum) throws SQLException {
				
				Image image = new Image();
				image.setUrl(rs.getString("url"));
				
				return image;
			}
		};
		
		return this.getJdbcTemplate().query(sql, mapper);
	}

	public Image findById(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Image findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

}
